package xyz.poke.business.tool.utils;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.poke.business.tool.dto.UserDTO;

/**
 * @author 迫壳
 * @date 2024/3/23 10:23
 */
public class TokenUtils {

    /**
     * 过期时间为半天 （12h）
     */
    private final static Long TIME = 60 * 12L;

    /**
     * 创建token
     * @param userDTO
     * @return
     */
    public static String createToken(UserDTO userDTO){
        String token = SecureUtil.md5(userDTO.getUsername()+ userDTO.getPassword()+ System.currentTimeMillis());
        RedisUtils redisUtils = SpringContextUtils.getContext().getBean(RedisUtils.class);
        //存储到redis中
        redisUtils.setEx(token, JSON.toJSONString(userDTO), TIME);
        return token;
    }

}
